﻿using System;
using System.Data;
using VegetableSale.NET.Utils;
using VegetableSale.NET.Entities;
using VegetableSale.NET.Entities.EGooden;

namespace VegetableSale.NET.Dao;

public class GoodMapper
{
    public List<Gooden> getGoodList(QueryDTO queryDTO)
    {
        String sql = "";

        if (queryDTO.Keyword.Equals(""))
        {
            sql = "select * from good;";
        }
        else
        {
            sql = "SELECT * FROM good WHERE commname='" + queryDTO.Keyword + "';";
        }

        List<Gooden> goods = new List<Gooden>();

        DataSet dateSet = MySqlHelper.GetDataSet(sql);
        DataTable dataTable = dateSet.Tables[0];

        foreach (DataRow row in dataTable.Rows)
        {
            uint commid = (uint) row[0];
            string commname = (string) row[1];
            double commoney = (double) row[2];
            string commdesc = (string) row[3];
            int providerid = (int) row[4];
            string commbering = (string) row[5];
            string size = (string) row[6];
            double inventory = (double) row[7];
            string picture = (string) row[8];
            Gooden good = new Gooden(commid, commname, commoney, commdesc, providerid, commbering, size, inventory,
                picture, "生鲜");
            goods.Add(good);
        }

        return goods;
    }

    public void addGood(GoodQuery goodQuery)
    {
        string commbering = RandomStringUtils.GetRandomStr("", 8);
        string picture = "/upload/" + goodQuery.Picture;
        string size = goodQuery.Size + "g";

        string sql =
            "INSERT INTO good (commname,commoney,commdesc,providerid,commbering,size,inventory,picture) VALUES ('" +
            goodQuery.Commname + "'," + goodQuery.Commoney + ",'" + goodQuery.Commdesc + "'," + goodQuery.Providerid +
            ",'" + commbering + "','" + size + "',0,'" + picture + "')";

        MySqlHelper.ExecuteSql(sql);
    }

    public void deleteGood(int commid)
    {
        string sql = "DELETE FROM good WHERE commid=" + commid + ";";

        MySqlHelper.ExecuteSql(sql);
    }

    public void updateGood(Gooden gooden)
    {
        string[] strArr = gooden.Picture.Split("/");
        string picture = "";
        Console.WriteLine("0:" + strArr[0]);
        //Console.WriteLine("1:"+strArr[1]);
        //Console.WriteLine(strArr[1]);
        if (strArr.Length == 1)
        {
            picture = "/upload/" + gooden.Picture;
        }
        else
        {
            picture = gooden.Picture;
        }

        Console.WriteLine("picture:" + picture);
        string sql = "UPDATE good SET commname='" + gooden.Commname + "',commdesc='" + gooden.Commdesc + "',commoney=" +
                     gooden.Commoney + ",providerid=" + gooden.Providerid + ",size='" + gooden.Size + "',picture='" +
                     picture + "' WHERE commid=" + gooden.Commid + ";";

        MySqlHelper.ExecuteSql(sql);
    }

    public double getInventoryById(int commid)
    {
        string sql = "SELECT inventory FROM good WHERE commid=" + commid + ";";

        double inventory = 0;
        
        DataSet dateSet = MySqlHelper.GetDataSet(sql);
        DataTable dataTable = dateSet.Tables[0];

        foreach (DataRow row in dataTable.Rows)
        {
            inventory = (double) row[0];
        }

        return inventory;
    }

    public void updateInventory(int commid,double inventory)
    {
        string sql = "UPDATE good SET inventory=" + inventory + " WHERE commid=" + commid + ";";

        MySqlHelper.ExecuteSql(sql);
    }

    public string getCommnameById(int commid)
    {
        string sql = "SELECT commname FROM good WHERE commid=" + commid + ";";

        string commname = "";
        DataSet dataSet = MySqlHelper.GetDataSet(sql);
        DataTable dataTable = dataSet.Tables[0];

        foreach (DataRow row in dataTable.Rows)
        {
            commname = (string) row[0];
        }

        return commname;
    }
}